package com.brillilab.common.entity;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.exceptions.BrillilabException;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求响应VO
 *
 * @author wmh
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseVo implements Serializable {
	private static final long serialVersionUID = 1L;

	/** 响应码 */
	private Integer code;

	/** 响应信息 */
	private String message;

	/** 响应数据 */
	private Object data;

	/**
	 * 数据不为null返回成功，否则返回失败
	 * @param data
	 * @return
	 */
	public static ResponseVo notNull(Object data) {
		if(data!=null){
			return success(data);
		}else{
			return failure(ResultEnum.DATA_NOT_EXIST);
		}
	}
	
	/**
	 * 成功
	 */
	public static ResponseVo success() {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.SUCCESS.getCode());
		responseVo.setMessage(ResultEnum.SUCCESS.getMessage());
		return responseVo;
	}

	public static ResponseVo success(Object data) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.SUCCESS.getCode());
		responseVo.setMessage(ResultEnum.SUCCESS.getMessage());
		responseVo.setData(data);
		return responseVo;
	}

	public static ResponseVo success(Object data, String msg) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.SUCCESS.getCode());
		responseVo.setMessage(msg);
		responseVo.setData(data);
		return responseVo;
	}

	public static ResponseVo successMap(String key, Object value) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.SUCCESS.getCode());
		responseVo.setMessage(ResultEnum.SUCCESS.getMessage());
		Map<String, Object> map = new HashMap<>();
		map.put(key, value);
		responseVo.setData(map);
		return responseVo;
	}

	/**
	 * 失败
	 */
	public static ResponseVo failure() {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.FAILURE.getCode());
		responseVo.setMessage(ResultEnum.FAILURE.getMessage());
		return responseVo;
	}

	public static ResponseVo failure(String msg) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.FAILURE.getCode());
		responseVo.setMessage(msg);
		return responseVo;
	}

	public static ResponseVo failure(ResultEnum resultEnum) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(resultEnum.getCode());
		responseVo.setMessage(resultEnum.getMessage());
		return responseVo;
	}

	public static ResponseVo failure(ResultEnum resultEnum, String message , Object data) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(resultEnum.getCode());
		responseVo.setMessage(StringUtils.isNotBlank(message) ? message : resultEnum.getMessage());
		responseVo.setData(data);
		return responseVo;
	}

	public static ResponseVo failureMap(String key, Object value) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(ResultEnum.FAILURE.getCode());
		responseVo.setMessage(ResultEnum.FAILURE.getMessage());
		Map<String, Object> map = new HashMap<>();
		map.put(key, value);
		responseVo.setData(map);
		return responseVo;
	}

	public static ResponseVo failureMap(ResultEnum resultEnum,String key, Object value) {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setCode(resultEnum.getCode());
		responseVo.setMessage(resultEnum.getMessage());
		Map<String, Object> map = new HashMap<>();
		map.put(key, value);
		responseVo.setData(map);
		return responseVo;
	}

	public static ResponseVo exception(Exception e) {
		ResponseVo responseVo = new ResponseVo();
		if (e instanceof BrillilabException) {
			BrillilabException e1 = (BrillilabException) e;
			responseVo.setCode(e1.getCode());
			responseVo.setMessage(StringUtils.isNotBlank(e1.getMessage()) ? e1.getMessage() : "系统繁忙，请稍后重试");
		} else {
			responseVo.setCode(ResultEnum.FAILURE.getCode());
			responseVo.setMessage(StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : "系统繁忙，请稍后重试");
		}
		return responseVo;
	}

}
